//
//  PayResultViewController.m
//  Rong360
//
//  Created by tianliwei on 9/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "PrepayResultViewController.h"
#import "PayResultCell.h"
#import "PreDateCell.h"
#import "WebViewController.h"
@interface PrepayResultViewController ()
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *subTitles;
@property (nonatomic, strong) NSArray *results;
@end

static NSString *CellIdentifier = @"Cell";
static NSString *PreDateCellIdentifier = @"PreDateCell";

@implementation PrepayResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithResults:(NSArray *)results{
    self = [super init];
    if (self) {
        self.results = results;
    }
    return self;
}

- (void)initTableFooterView{
    UIView *tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 100)];
    UILabel *label = [UILabel labelWithFrame:CGRectMake(15, 10, 300, 15) title:@"以上结果仅供参考，请以当地实际情况为准" fontColor:[UIColor lightGrayColor] fontSize:10];
    UIButton *reCal = [UIButton buttonWithTag:1 frame:CGRectMake(15, 45, 135, 35) normalImage:[UIImage imageNamed:@"btn_qd_normal"] highlightedImage:[UIImage imageNamed:@"btn_qd_focus"] target:self action:@selector(buttonClicked:)];
    [reCal setTitle:@"重新计算" forState:UIControlStateNormal];
    [reCal.titleLabel setFont:[UIFont systemFontOfSize:14]];
    UIButton *sendResult = [UIButton buttonWithTag:2 frame:CGRectMake(170, 45, 135, 35) normalImage:[UIImage imageNamed:@"btn_tg_normal"] highlightedImage:[UIImage imageNamed:@"btn_tg_focus"] target:self action:@selector(buttonClicked:)];
    [sendResult setTitle:@"发送结果" forState:UIControlStateNormal];
    [sendResult.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [tableFooterView addSubview:label];
    [tableFooterView addSubview:reCal];
    [tableFooterView addSubview:sendResult];
    self.tableView.tableFooterView = tableFooterView;
}

- (void)buttonClicked:(UIButton *)sender{
    switch (sender.tag) {
        case 0:{

        }
        case 1:{
            [self.navigationController popViewControllerAnimated:YES];
            break;
        }
        case 2:{
            NSString *shareText = [NSString stringWithFormat:@"我在#融360买房助手#中测得提前还款可以节省%@元利息。这个应用真的很强大，用了之后，基本不怕被中介忽悠了，你也来试试吧",self.results[0]];
            
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialWechatHandler setWXAppId:kWXAppID url:kRong360Host];
            [UMSocialConfig setQQAppId:kTencentID url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
//            [UMSocialConfig setShareQzoneWithQQSDK:YES url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
            [UMSocialSnsService presentSnsIconSheetView:self
                                                 appKey:kUMSocialKey
                                              shareText:shareText
                                             shareImage:[UIImage imageNamed:kShareIcon]
                                        shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                               delegate:nil];
            break;
        }
        default:
            break;
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSInteger rowHeight = 50;
    self.tableView.rowHeight = rowHeight;
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"PayResultCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"PreDateCell" bundle:nil] forCellReuseIdentifier:PreDateCellIdentifier];
    [self.view addSubview:self.tableView];
    [self initTableFooterView];
    self.title = @"计算结果";
    self.titles = @[@"节省利息:",@"该月总还款额:",@"新月还款额:",@"原月还款额:",@"新最后还款期:",@"原最后还款期:",@"已还总额:",@"已还利息"];
    self.subTitles = @[@"元",@"元",@"元",@"元",@"",@"",@"元",@"元"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.titles.count;
}

//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    return @"";
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 4 || indexPath.row == 5) {
        PreDateCell *cell = [tableView dequeueReusableCellWithIdentifier:PreDateCellIdentifier];
        cell.mainLabel.text = self.titles[indexPath.row];
        cell.subLabel.text = self.results[indexPath.row];
        cell.subLabel.textColor = APPCOLOR;
        return cell;
    }else{
        PayResultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.mainLabel.text = self.titles[indexPath.row];
        cell.subLabel.text = self.results[indexPath.row];
        if (indexPath.row == 0) {
            cell.subLabel.textColor = [UIColor redColor];
        }else{
            cell.subLabel.textColor = APPCOLOR;
        }
        cell.unitLabel.text = self.subTitles[indexPath.row];
        return cell;
    }
    return nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
